package com.tan.lbs.activity;

import java.util.Iterator;
import java.util.List;

import com.tan.lbs.activity.R;
import com.tan.lbs.dao.GameDAO;
import com.tan.lbs.dao.ItemDAO;
import com.tan.lbs.dao.QuestDAO;
import com.tan.lbs.dao.impl.GameDAOImpl;
import com.tan.lbs.dao.impl.ItemDAOImpl;
import com.tan.lbs.dao.impl.QuestDAOImpl;
import com.tan.lbs.entity.Game;
import com.tan.lbs.entity.GameHeader;
import com.tan.lbs.entity.Item;
import com.tan.lbs.entity.Quest;
import com.tan.lbs.util.Constants;
import com.tan.lbs.util.ResourceReader;
import com.tan.lbs.util.URLs;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ItemsTabActivity extends ListActivity{
	
	private String gameID;
	List<Item> items;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.itemstab);
		gameID  = getSharedPreferences(Constants.PREFS_NAME, 0).getString("currentGameID", "");
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		super.onStart();
		ItemDAO itemDAO = new ItemDAOImpl(this);
		List<Item> items = itemDAO.retreiveItems(gameID);
		Iterator<Item> it =items.iterator(); 
		while(it.hasNext()) {
			Item item = it.next();
			if(!item.getStatus().equals(Item.PICKED)) {
				it.remove();
			}
		}
		ArrayAdapter adapter = new ItemListRowAdapter(this,R.layout.itemlist_item,items);
		setListAdapter(adapter);
	}
	
	private class ItemListRowAdapter extends ArrayAdapter<Item> {



		public ItemListRowAdapter(Context context, int textViewResourceId,
				List<Item> items) {
			super(context, textViewResourceId, items);
			ItemsTabActivity.this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;

			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.itemlist_item, null);
			}
			
			
			final Item item = items.get(position);
			/*if(item.getStatus().equals(Item.PICKED)) {
				view.setVisibility(View.VISIBLE);
			} else {
				view.setVisibility(View.GONE);
			}*/
			if (item != null) {
				Bitmap icon = ResourceReader.readImage(ItemsTabActivity.this,
						URLs.gameResourceLoc + gameID + "/"
								+ item.getIconID());

				ImageView itemIcon = (ImageView) view
						.findViewById(R.id.itemicon);
				itemIcon.setImageBitmap(icon);

				TextView itemName = (TextView) view.findViewById(R.id.itemname);
				itemName.setText(item.getTitle());

			}
			return view;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		final Item item = items.get(position);
		Intent intent = new Intent();
		intent.putExtra("itemID", item.getItemID());
		intent.setClass(ItemsTabActivity.this,
				ItemDetailActivity.class);
		ItemsTabActivity.this.startActivity(intent);
	}
}
